package com.yue.jersey.practice.resource;

import com.yue.jersey.practice.model.CountryModel;
import com.yue.jersey.practice.model.SNameModel;
import com.yue.jersey.practice.provider.CountryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/12/2<br/>
 *
 * @author QiuYang
 */
@Path("country")
@Component
public class CountryResource {

    @Autowired
    private CountryProvider countryProvider;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response allCountries(){
        List<CountryModel> models = countryProvider.allCountries();

        return Response.ok(models).build();
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteCountriesBySName(@Valid @NotNull SNameModel model){

        List<SNameModel.NameModel> list = model.getLists();

        return Response.noContent().build();
    }

    /**
     * List<Object>   args likes : [{}, {}, ..., {}]
     * Object{
     *     List<Object2>
     * }    args likes : {object:[{}, {}, ..., {}]}
     * @param snames
     * @return
     */
    @PUT
    @Path("snames")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateCountriesWithSNames(List<String> snames){  // 参数like: ["BE","BF","BG","HK"]
        List<CountryModel> models = countryProvider.updateCountriesWithSNames(snames);

        return Response.ok(models).build();
    }

}
